package Enemies;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import java.awt.Color;
import java.util.ArrayList;

import Manage.Counter;
public class Enemy extends Actor{
	private int currentHealth, maxHealth;
	//the speed can be negative or positive. 
	//positive speed is move x spaces per act
	//negative: move x/speed spaces per act
		//note: speed of -1 = speed of 1
		//ex. speed of -4 = speed of 1/4
	protected int speed;
	protected int speedStep;
	protected int moveLoc;
	protected ArrayList<Location> path;
	public Enemy(ArrayList<Location> path){
		this.maxHealth = 60;
		currentHealth = maxHealth;
		this.speed = 1;
		this.path = path;
		setHealthColor();
		moveLoc = 0;
	}
	public Enemy(int health, ArrayList<Location> path){
		this.maxHealth = health;
		currentHealth = maxHealth;
		this.speed = 1;
		this.path = path;
		setHealthColor();
		moveLoc = 1;
	}
	public void setStats(int mult){
		setMaxHealth(maxHealth + (int)(maxHealth * mult / 4));
		//setSpeed(getSpeed() + (int)(speed * mult / 4));
	}
	public int getMoveLoc(){
		return moveLoc;
	}
	public void setHealthColor(){
		if(currentHealth <= maxHealth * 0.2){
			setColor(Color.RED);
		}else if(currentHealth <= maxHealth * 0.4){
			setColor(Color.ORANGE);
		}else if(currentHealth <= maxHealth * 0.6){
			setColor(Color.YELLOW);
		}else if(currentHealth <= maxHealth * 0.8){
			setColor(Color.CYAN);
		}else{
			setColor(Color.GREEN);
		}
	}
	public void setSpeed(int speed){ 
		this.speed = speed; 
	}
	public int getSpeed(){ 
		return speed; 
	}
	public void setMaxHealth(int health){ 
		this.maxHealth = health; 
		setCurrentHealth(health);
	}
	//cant overheal
	public void setCurrentHealth(int health){ 
		if(health > maxHealth){
			health = maxHealth;
		}
		this.currentHealth = health; 
		setHealthColor();
	}
	public int getCurrentHealth(){ 
		return currentHealth; 
	}
	public int getMaxHealth(){
		return maxHealth;
	}
	public void act(){ 
		if(getLocation() != null){
			if(getLocation().equals(path.get(path.size()-1))){ 
				removeSelfFromGrid(getLocation());
				Counter.takeLife();
			}else{
				if(speed >= 0){
					for(int i = 0; i < speed; i++){
						if(getLocation().equals(path.get(path.size()-1))){ 
							removeSelfFromGrid(getLocation());
							Counter.takeLife();
							break;
						}
						move();
						moveLoc++;
					}
				}else{
					speedStep--;
					if(speedStep <= speed){
						move();
						moveLoc++;
						speedStep = 0;
					}
				}
				 
			}
		}	
	}
	public void move(){
		//setDirection(getLocation().getDirectionToward(path.get(moveLoc)));
		Location loc = path.get(moveLoc);
		int row = loc.getRow();
		int col = loc.getCol();
		int dep = getGrid().getNumDep() - 1;
		loc = new Location(row, col, dep);
		while(dep >= 0){ //1 less than depth
			if(!(getGrid().get(loc) instanceof Enemy)){
				break;
			}
			dep--;
			loc = new Location(row, col, dep);
			
		}
		path.set(moveLoc, loc);
		moveTo(path.get(moveLoc));
	}
	public void takeDamage(){
		currentHealth--;
		setHealthColor();
		if(currentHealth <= 0){
			die();
		}
	}
	public void takeDamage(double damage){
		for(int i = 0; i < damage; i++){ 
			takeDamage();
			if(currentHealth<= 0){
				
			}
			if (currentHealth <= 0){
				break;
			}
			if(getLocation() == null){
				return;
			}
		}
	}
	public void die(){
		removeSelfFromGrid(getLocation());
		Counter.addKill();
		Counter.addMoney(getMaxHealth()/2);
		removeSelfFromGrid(getLocation());
		//add maxHealth/2
	}
}

